const router = require("koa-router")();
const {
  getAllShop,
  getOneShop,
  getItemByTab,
  getHotWords,
} = require("../control/shopControl");
const { SuccessModel, ErrorModel } = require("../messageModel/index");
const loginCheck = require("../middleware/loginCheck");

router.prefix("/api/shop");

//查看所有店铺信息
router.get("/hot-list", async (ctx, next) => {
  const res = await getAllShop();
  if (res) {
    ctx.body = new SuccessModel(res);
  } else {
    ctx.body = new ErrorModel("无法获得所有商铺信息");
  }
});

//查看单个店铺信息
router.get("/:id", async (ctx, next) => {
  const id = ctx.params.id;
  const res = await getOneShop(id);
  if (res) {
    ctx.body = new SuccessModel(res);
  } else {
    ctx.body = new ErrorModel("无法获得当前商铺信息");
  }
});

//查询单个店铺某一类商品
router.get("/:id/product", async (ctx, next) => {
  const id = ctx.params.id;
  const { tab } = ctx.query;
  const res = await getItemByTab(id, tab);
  if (res.length) {
    ctx.body = new SuccessModel(res);
  } else {
    ctx.body = new ErrorModel("无法获得该类产品");
  }
});

//获取热词
router.get("/search/hot-words", async (ctx, next) => {
  const res = await getHotWords();
  if (res) {
    ctx.body = new SuccessModel(res);
  } else {
    ctx.body = new ErrorModel("获取热词失败");
  }
});

module.exports = router;
